package com.tueye.dao.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tueye.base.util.BaseDao;
import com.tueye.base.util.IBaseHbnDao;
import com.tueye.base.util.Page;
import com.tueye.dao.ITopicDao;
import com.tueye.entity.Comment;
import com.tueye.entity.Pictures;
import com.tueye.entity.TopicType;
import com.tueye.entity.Topics;
import com.tueye.entity.user.Flowers;
import com.tueye.entity.user.User;
import com.tueye.entity.user.UserCollectionInfo;
import com.tueye.utils.FileHandle;
import com.tueye.utils.TuEyeConstant;

/**
 * 
 * @author hanxiaowei
 * 
 */
@Scope("prototype")
@Component("topicDao")
public class ITopicDaoImpl extends BaseDao implements ITopicDao {

	@Autowired
	@Qualifier("baseHbnDao")
	private IBaseHbnDao baseHbnDao;

	@SuppressWarnings("unchecked")
	@Override
	public List<TopicType> getAllTopicType() {
		// TODO Auto-generated method stub
		String hql = "from TopicType";
		return baseHbnDao.queryList(hql);
	}

	@Override
	public TopicType getTopicTypeById(String id) {
		// TODO Auto-generated method stub
		return (TopicType) baseHbnDao.loadById(TopicType.class, id);
	}

	@Override
	public void addTopicType(TopicType tt) {
		// TODO Auto-generated method stub
		baseHbnDao.save(tt);
	}

	@Override
	public void saveTopics(Topics topic) {
		// TODO Auto-generated method stub
		baseHbnDao.saveOrUpdate(topic);
	}

	@Override
	public void savePicture(Pictures picture) {
		// TODO Auto-generated method stub
		baseHbnDao.save(picture);
	}

	@Override
	public Topics getTopicById(String id) {
		// TODO Auto-generated method stub
		return (Topics) baseHbnDao.loadById(Topics.class, id);
	}

	@Override
	public Page pagedQueryByHQL(String hql, int pageNo, int pageSize,
			Object... values) {
		// TODO Auto-generated method stub
		return baseHbnDao.pagedQueryByHQL(hql, pageNo, pageSize, values);
	}

	@Override
	public List<Comment> getAllCommentsByPId(String picid) {
		// TODO Auto-generated method stub
		return baseHbnDao.queryList("from Comment t where t.picture.id='"
				+ picid + "' order by t.sys_time desc");
	}

	@Override
	public Pictures getPictureById(String picid) {
		// TODO Auto-generated method stub
		return (Pictures) baseHbnDao.loadById(Pictures.class, picid);
	}

	@Override
	public void saveComment(Comment comment) {
		// TODO Auto-generated method stub
		baseHbnDao.save(comment);
	}

	@Override
	public void delTopic(String id) {
		Topics topic = (Topics) baseHbnDao.findById(Topics.class, id);
		baseHbnDao.delete(topic);
		//List<Pictures> pictureList = new ArrayList<Pictures>();
		//删除图片文件夹
		//pictureList = topic.getPictures();
//		for (int i = 0; i < pictureList.size(); i++) {
//			Pictures picture = pictureList.get(i);
			String filepath = TuEyeConstant.PROJECT_TOPIC_PATH
					+ File.separatorChar
					+ topic.getTopic_type().getType_enlish_name()
					+ File.separatorChar + topic.getId();
			 File file = new File(filepath);
			if(file.exists()){
		      	FileHandle.delAllFile(filepath);
		      	FileHandle.delFolder(filepath);
			}
		//}
	}

	@Override
	public void delePictures(String hql) {
		this.baseHbnDao.delete(hql);

	}

	@Override
	public void delePic(String id, String picpath) {
		Pictures pic = (Pictures)baseHbnDao.findById(Pictures.class, id);
		baseHbnDao.delete(pic);
		File file = new File(picpath);
		if(file.exists()){
			file.delete();
		}
		
	}

	@Override
	public void saveTopicCollect(UserCollectionInfo collectinfo) {
		// TODO Auto-generated method stub
		baseHbnDao.save(collectinfo);
		
	}

	@Override
	public void saveFlower(Flowers flower) {
		// TODO Auto-generated method stub
		baseHbnDao.saveOrUpdate(flower);
	}
	public List getFlowerByTopicsId(String topicsId) {
		// TODO Auto-generated method stub
		String hql = "from Flowers flow where flow.topics.id = '" + topicsId+ "'";
		return baseHbnDao.queryList(hql);
	}

	@Override
	public void updateTopic(Topics topic) {
		// TODO Auto-generated method stub
		baseHbnDao.saveOrUpdate(topic);
	}

	@Override
	public int getCommentNumByTopicId(String topicId) {
		// TODO Auto-generated method stub
		List l = baseHbnDao.queryList("select count(*) from Comment c where c.picture.id in (select id from Pictures p where " +
 		"p.topics.id = '"+topicId+"')");
		return  new Integer( l.get(0).toString());
	}

	@Override
	public User getUserByTopic(Topics topic) {
		// TODO Auto-generated method stub
		  User u = null;
		 String hql = "select user from UserCollectionInfo t where t.userTopicType=1 and t.topics.id = '"+topic.getId()+"'";
		 List l = baseHbnDao.queryList(hql);
		 if(l != null) {
			u = (User) l.get(0);
		}
		return u;
	}

	@Override
	public List queryBysql(String sql) {
		// TODO Auto-generated method stub
		return baseHbnDao.queryBySqlList(sql);
	}

	@Override
	public List<Topics> getTopicsByHql(String hql) {
		// TODO Auto-generated method stub
		return baseHbnDao.queryList(hql);
	}

	@Override
	public Map<String, Object> getFlowerNum(String topicid) {
		Map<String, Object> FlowerNumArry = new HashMap<String, Object>();
		List sumNum = new ArrayList();
		String flowersNumsql = "select sum(flowersNum) from com.tueye.entity.user.Flowers f where f.topics ='"+topicid+"'";
		sumNum = baseHbnDao.queryList(flowersNumsql);
		String flowersNum =getObjectString(sumNum,"flowersNum");
		FlowerNumArry.put("flowersNum", flowersNum);
		
		//有才个数
		String CaiNumsql = "select sum(caiNum) from com.tueye.entity.user.Flowers f where f.topics ='"+topicid+"'";
		sumNum = baseHbnDao.queryList(CaiNumsql);
		String CaiNum =getObjectString(sumNum,"caiNum");
		FlowerNumArry.put("caiNum", CaiNum);
		return FlowerNumArry;
	}
	private String getObjectString(List ls,String objectname ){
		
		if (ls.isEmpty()) {
			return "0";
		}
		if(ls.get(0) == null){
			
			return "0";
			
		}else{
			return ls.get(0).toString();
		}
	}

	@Override
	public String getClickNumByTopicId(String topicid) {
		String clickNum = "0";
		List result = baseHbnDao.queryList("select topic_clickCount from Topics t where t.id='"+topicid+"'");
		if(result.isEmpty()){
			return clickNum;
		}else{
			Integer count = (Integer) result.get(0);
			if(null == count) {
				return "0";
			}
			return count.toString();
			//return count;
		//	return "0";
		}
	}

}
